<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use DB;
use App\Models\Type;

class IndexController extends Controller
{
   public function index()
   {  
    //首页显示数据库中的信息

      $pro = DB::table('product')->get();
      $links=DB::table('links')->get();

      $dbs = DB::table('product');
      //教材
      $jc=$dbs
     
      ->where('path_id','7')
       
      ->get()->all();
      
      //文学
      
      $wx=DB::table('product')->where('path_id','12')->get()->all();
      //中国古典小说
     $zggd=DB::table('product')->where('path_id','99')->get()->all();
     //外国
      $wg=DB::table('product')->where('path_id','103')->get()->all();

      //医学
      $yx=DB::table('product')->where('path_id','38')->get()->all();
      // 古籍
      $gj=DB::table('product')->where('path_id','19')->get()->all();
      

    

     
      $links=DB::table('links')->get();
      $type=DB::table('type')->get()->all();
      // 小说
      $xiaoshuo=DB::table('type')->where('parint_id','2')->get()->all();
      // 教育
      $jiaoyu=DB::table('type')->where('parint_id','1')->get()->all();
      // 文艺
      $wenyi=DB::table('type')->where('parint_id','3')->get()->all();
      // 人文
      $renwen=DB::table('type')->where('parint_id','5')->get()->all();
       $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
      
       //首页资讯
       $zx=DB::table('news')->where('sticky','2')->get()->all();
 
      // dd($pro);
      return view('home.index', ['pro'=>$wg,'links'=>$links,
        'search'=>$ss,
        'xiaoshuo'=>$xiaoshuo,
        'zx'=>$zx,
        'jiaoyu'=>$jiaoyu,
        'wenyi'=>$wenyi,
        'renwen'=>$renwen,
        'zggd'=>$zggd,
         'all'=>$type,
         'wx'=>$wx,
         'wg'=>$wg,
         'jc'=>$jc,
         'gj'=>$gj

  
        ]);
   }


   public function doindex($id, Request $request)
   {  

    

    //类别
    $type=DB::table('type')->get()->all();
    // 搜索关键字
       $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
    $links=DB::table('links')->get();

    //商品的详细信息 
    // dd($request);
      $list = DB::table('product')
        ->join('book', 'product.id', '=', 'book.product_id')

        ->select('product.*', 'book.author', 'book.publishing', 'book.publish_time', 'book.isbn', 'book.language', 'book.which_edtion', 'book.total_page', 'book.bind_layout', 'book.book_size', 'book.editor_description', 'book.book_summary', 'book.author_summary', 'book.print_time', 'book.print_number', 'book.paper_type', 'book.print_frequency', 'book.gross', 'book.catalog')
        ->where('product.id',$id)
        ->first();
        // dd($list);
      // dd($list->picture);
        // dd($list);
      $pic = $list->picture;
      // dd($pic);

      //产品名

      $proname = $list->name;
      // dd($proname);
      //产品价格
      $proprice = $list->lower_price;
      //
      $db = DB::table('browse_log')->where('product_id',$id)->get()->count();
      if($db < 1){
        //点击产品插入浏览日志表
        if (session()->has('homeuser')){
          // dd(session('homeuser')->id);
          //用户id
          $logid = session('homeuser')->id;
          $log = DB::table('browse_log')->insertGetId(
            ['user_id' => "$logid", 'product_id' => "$id", 'pic' => "$pic", 'product_name' => "$proname", 'product_price' => "$proprice"]
          );
          //浏览记录只保留20条数据
          if($log > 20){
            $num = $log-20;
            $log = DB::table('browse_log')->where('id','<',"$num")->delete();
          }
        }else{
          $log = DB::table('browse_log')->insertGetId(
            ['product_id' => "$id", 'pic' => "$pic", 'product_name' => "$proname", 'product_price' => "$proprice"]
          );
          //浏览记录只保留20条数据
          if($log > 20){
            $num = $log-20;
            $log = DB::table('browse_log')->where('id','<',"$num")->delete();
          }
        }
      }
      //获取浏览日志表中的商品
      $browse_log = DB::table('browse_log')->orderBy('id', 'desc')->take(3)->get();
      // dd($browse_log);

      //获取商品推荐表中的商品
      $rec = DB::table('recommend')
        ->join('product', 'product.id', '=', 'recommend.product_id')
        ->select('recommend.*', 'product.name', 'product.keywords', 'product.picture', 'product.lower_price')
        ->orderBy(\DB::raw('RAND()'))
        ->take(2)
        ->get();

      //统计评论总数
      $eval = DB::table('product_evaluation')->where('product_id',$id)->count();
      //好评数
      $a = DB::table('product_evaluation')->where([
          ['title','好评'],
          ['product_id',$id],
      ])->count();
      
      //中评数
      $b = DB::table('product_evaluation')->where([
          ['title','中评'],
          ['product_id',$id],
      ])->count();
      //差评数
      $c = DB::table('product_evaluation')->where([
          ['title','差评'],
          ['product_id',$id],
      ])->count();
      // dd($list);
      // 分页 
      // 跳转商品详情页
      $user = DB::table('product_evaluation')
          ->join('user', 'product_evaluation.user_id', '=', 'user.id')
          ->join('evaluation_reply', 'product_evaluation.order_id', '=', 'evaluation_reply.evaluation_id')
          ->select('product_evaluation.*', 'user.nick_name', 'evaluation_reply.reply_content', 'evaluation_reply.reply_time')
          ->where('product_evaluation.product_id', $id)
          ->orderBy('evaluation_time', 'desc')
          ->paginate(2);
      return view('home.product',['links'=>$links, 
          'search'=>$ss,
          'all'=>$type,
          'rec'=>$rec, 
          'browse'=>$browse_log,
          'user'=>$user, 
          'list'=>$list, 
          'eval'=>$eval, 
          'a'=>$a,
          'b'=>$b,
          'c'=>$c]);
   }
   

    public function gouserinfo()
    {
       return view('home.userinfo');     
    } 



}